001 /**
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: Apr 27, 2003
005 * Time: 9:18:07 PM
006 */
007
008 package EVolve.util.phasedetectors;
009
010 import EVolve.visualization.XYViz.XYVisualization;
011 import EVolve.visualization.Visualization;
012 import EVolve.Scene;
013 import EVolve.util.HelperFuncs;
014 import java.util.*;
015
016 public class PhaseClipboard {
017 private ArrayList copiedPhase;
018 private XYVisualization source;
019 private HashSet targetList;
020 private ArrayList sourceTimeMap, targetTimeMap;
021 private int sourceInterval, targetInterval;
022 private boolean isEmpty;
023
024 public PhaseClipboard() {
025 isEmpty = true;
026 targetList = new HashSet();
027 }
028
029 public void copyPhase() {
030 ArrayList phaseList = null;
031 source = HelperFuncs.getActiveXYViz();
032
033 if ((source == null)||(source.getPhaseDetector()==null)) {
034 Scene.showErrorMessage("No phase information available.");
035 return;
036 }
037
038 sourceTimeMap = source.getTimeMap();
039 sourceInterval = source.getInterval();
040 phaseList = source.getPhaseDetector().getPhase();
041 if (phaseList.size() == 0) {
042 Scene.showErrorMessage("No phase information available.");
043 return;
044 }
045
046 copiedPhase = HelperFuncs.cloneArrayList(phaseList);
047 if (copiedPhase.size() > 0) isEmpty = false;
048 }
049
050 public void pastePhase() {
051 XYVisualization target = null;
052
053 if (copiedPhase == null) {
054 Scene.showErrorMessage("No phase data available, please copy phase first.");
055 return;
056 }
057
058 target = HelperFuncs.getActiveXYViz();
059 if (target == null) {
060 Scene.showErrorMessage("Can not paste phase to non-HotSpotVisualization.");
061 return ;
062 }
063
064 if (source.getDataSourceId() != target.getDataSourceId()) {
065 Scene.showErrorMessage("Can not paste phases to a visualization generated from a different data source");
066 return;
067 }
068
069 PhaseDetector detector = target.getPhaseDetector();
070 if (detector == null) return;
071
072 if (!targetList.contains(target))
073 targetList.add(target);
074
075 targetTimeMap = target.getTimeMap();
076 targetInterval = target.getInterval();
077 ArrayList phases = translatePhase();
078
079 detector.pastePhases(phases);
080 }
081
082 public boolean isEmpty() {
083 return isEmpty;
084 }
085
086 private ArrayList translatePhase() {
087 ArrayList translated = new ArrayList();
088 ArrayList eventNoList = new ArrayList();
089
090 for (int i=0; i<copiedPhase.size(); i++) {
091 Integer phase = (Integer)copiedPhase.get(i);
092 long event = findEventNo(sourceTimeMap, phase.intValue()+1,sourceInterval);
093 eventNoList.add(new Long(event));
094 }
095
096 for (int i=0; i<eventNoList.size(); i++) {
097 Long event = (Long)eventNoList.get(i);
098 long time = findTime(targetTimeMap, event.longValue());
099 translated.add(new Integer((int)(time/targetInterval)));
100 }
101
102 return translated;
103 }
104
105 private long findTime(ArrayList timeMap, long eventNo) {
106 long prevEvent = 0, curEvent = 0;
107 long time = 0;
108
109 for (int i=0; i<timeMap.size(); i++) {
110 long[] time2Event = (long[])timeMap.get(i);
111 curEvent = time2Event[1];
112 time = time2Event[0];
113 if ((prevEvent == eventNo) || (curEvent == eventNo) ||
114 ((prevEvent < eventNo) && (eventNo < curEvent))) {
115 break;
116 }
117 prevEvent = curEvent;
118 }
119
120 return time;
121 }
122
123 private long findEventNo(ArrayList timeMap, int time, int interval) {
124 long prevTime = 0, curTime = 0;
125 long event = 0;
126
127 for (int i=0; i<timeMap.size(); i++) {
128 long[] time2Event = (long[])timeMap.get(i);
129 curTime = time2Event[0];
130 if ((prevTime/interval == time) || (curTime/interval == time) ||
131 ((prevTime/interval < time) && (time < curTime/interval))) {
132 break;
133 }
134 event = time2Event[1];
135 prevTime = curTime;
136 }
137
138 return event;
139
140 }
141 }